# Copyright (C) 2010 Nathan Grubb

from toasted.settings import SEND_CONTENT_TYPE_HEADER

class HttpResponse(object):
    """A representation of an HTTP response."""
    def __init__(self, headers=[], content=""):
        self.headers = headers
        self.content = content

    def get_response(self):
        """Returns an HTTP response, to be sent directly to the browser"""
        # This is defined here because we get a really weird bug where this
        # is added to the list multiple times, for every time 
        # toasted.template.render_to_response() is called.
        if SEND_CONTENT_TYPE_HEADER is True:
            self.headers.append("Content-Type: text/html")
        
        response = ""
        # A header doesn't have to be a string. toasted.cookie.Cookie, for example,
        # defines a __str__() that returns a useful string.
        for header in self.headers:
            response += str(header) + " \n"
        response += "\n"
        
        if self.content is not None:
            response += self.content
        return response
    
    def __str__(self):
        """An HttpResponse can be printed, which is useful if you want to
           access this object directly instead of using templates.
        """
        return str(self.get_response())